package xyz.msj.hrm.service.impl;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import xyz.msj.hrm.doc.CourseDocument;
import xyz.msj.hrm.query.CourseQuery;
import xyz.msj.hrm.repository.CourseElasticsearchRepository;
import xyz.msj.hrm.service.ICourseEelasticService;
import xyz.msj.hrm.util.PageList;

/**
 * 课程搜索
 */
@Service
public class CourseEelasticServiceImpl implements ICourseEelasticService {
    //es库
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    /**
     * 根据条件进行查询
     *
     * @param courseQuery
     * @return
     */
    @Override
    public PageList<CourseDocument> search(CourseQuery courseQuery) {
        //创建本地对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //创建组合查询对象
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (StringUtils.isNotEmpty(courseQuery.getKeyword())) {
            //根据字段searchField，进行关键字查询
            boolQuery.must(QueryBuilders.matchQuery("searchField", courseQuery.getKeyword()));
        }
        if (StringUtils.isNotEmpty(courseQuery.getCourseType())) {
            //根据课程类型id查询
            boolQuery.filter(QueryBuilders.termQuery("courseTypeId", courseQuery.getCourseType()));
        }
        if (StringUtils.isNotEmpty(courseQuery.getPriceMin())) {
            //大于等于最小价格
            boolQuery.filter(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin()));
        }
        if (StringUtils.isNotEmpty(courseQuery.getPriceMax())) {
            //小于等于最大价格
            boolQuery.filter(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMax()));
        }
        //查询条件
        builder.withQuery(boolQuery);
        //分页
        builder.withPageable(PageRequest.of(courseQuery.getPage()-1,courseQuery.getRows()));
        //把courseQuery中的排序设置给Builder
        if (StringUtils.isNotEmpty(courseQuery.getSortField())) {
            //声明变量接收对应的es排序查询参数
            String sortField = null;
            //使用switch进行循环设置排序参数
            switch(courseQuery.getSortField()){
                case "xl" : sortField = "saleCount"; break;
                case "xp" : sortField = "onLineTime"; break;
                case "pl" : sortField = "commentCount"; break;
                case "jg" : sortField = "price"; break;
                case "rq" : sortField = "viewCount"; break;
            }
            //排序默认降序
            SortOrder sortOrder = SortOrder.DESC;
            //判断排序方式，如何小写等于asc就是升序
            if (StringUtils.isNotEmpty(courseQuery.getSortType()) && courseQuery.getSortType().toLowerCase().equals("asc")) {
                sortOrder = SortOrder.ASC;
            }
            //排序
            builder.withSort(new FieldSortBuilder(sortField).order(sortOrder));

        }

        //5.执行查询得到结果Page对象
        Page<CourseDocument> page = courseElasticsearchRepository.search(builder.build());

        //6.把结果封装成PageList返回
        return new PageList<>(page.getTotalElements(), page.getContent());
    }
}
